// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// This file is part of CodingEditor.
// Note:	This project is derived from Peter Project
//			(hosted on sourceforge and codeplex)
//
// Copyright (c) 2008-2009, CE Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


namespace CE.Main
{
	using System;
	using System.ComponentModel;
	using System.Diagnostics;
	using System.Drawing;
	using System.Reflection;
	using System.Windows.Forms;
	using System.Xml;

	public sealed partial class UpdateChecker : CEBaseForm
	{
		#region Fields

		private BackgroundWorker worker;

		private Version newVersion;
		private string notes = "";
		private string url = "";
		private bool isSuccessfulXmlFileParse;

		public bool IsSuccessfulXmlFileParse
		{
			get {
				return isSuccessfulXmlFileParse;
			}
		}

		#endregion Fields

		#region Constructors

		public UpdateChecker()
		{
			InitializeComponent();
			worker = new BackgroundWorker();
			worker.RunWorkerCompleted += worker_RunWorkerCompleted;
			worker.DoWork += worker_DoWork;
			worker.RunWorkerAsync();
            System.Threading.Thread.Sleep(500);
            this.Paint += UpdateChecker_Paint;
		}

		#endregion Constructors

		#region Methods

		private void CompareVersions()
		{
			if (newVersion == null)
				return;

			Version currVersion = Assembly.GetExecutingAssembly().GetName().Version;
			if (currVersion.CompareTo(newVersion) <= 0) {
				this.Text = "Something new is there!";
				lblMessage.Text = "Go to the download page?";
				tbNotes.Text =  notes;
			} else {
				lblMessage.ForeColor = Color.Red;
				lblMessage.Text = "Currently no new versions are available \nor an error occured.";
				this.Text = "No Updates";
			}
		}

		private bool TryParseXmlFile()
		{
			XmlTextReader xmlReader = new XmlTextReader(CE.Classes.Constants.GetLatestVersionFileUrl);
            xmlReader.Read();
			if (xmlReader.NodeType == XmlNodeType.None) {
				xmlReader.Close();
				return false;
			}

			xmlReader.MoveToContent();
			string elementName = "";

			if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "CodingEditor")) {
				while (xmlReader.Read()) {
					if (xmlReader.NodeType == XmlNodeType.Element) {
						elementName = xmlReader.Name;
					} else if ((xmlReader.NodeType == XmlNodeType.Text) && (xmlReader.HasValue)) {
						switch (elementName) {
						    case "version":
							    newVersion = new Version(xmlReader.Value);
							    break;

						    case "url":
							    url = xmlReader.Value;
							    break;

						    case "notes":
							    notes = xmlReader.Value;
							    break;
						}
					}
				}
			}

			xmlReader.Close();

			return true;
		}

		private void UpdateChecker_FormClosing(object sender, FormClosingEventArgs e)
		{
			Dispose(true);
		}

		private void btnYes_Click(object sender, EventArgs e)
		{
			Process.Start(url);
			Close();
		}

		private void btnNo_Click(object sender, EventArgs e)
		{
			Close();
		}

		private void worker_DoWork(object sender, EventArgs e)
		{
			isSuccessfulXmlFileParse = TryParseXmlFile();
		}

		private void worker_RunWorkerCompleted(object sender, EventArgs e)
		{
			if (!isSuccessfulXmlFileParse) {
				Program.ShowError(CE.Classes.Constants.LatestVersionFileRetrieveError);
				return;
			}
			CompareVersions();
		}

        private void UpdateChecker_Paint(object sender, PaintEventArgs e)
        {
            Program.MakeBackgroundGradient(e.Graphics, this);
        }

		#endregion Methods
	}
}